package moni;

import jdk.swing.interop.SwingInterOpUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

public class Priority {
    public static void main(String[] args) {
//        PriorityQueue<Integer> pq = new PriorityQueue<>(Arrays.asList(1,2,4));
//        System.out.println(pq);
//        pq.offer(5);
//        pq.offer(5);
//        pq.offer(1);
//        pq.offer(9);
//        pq.offer(7);
//        System.out.println(pq);
//        System.out.println(pq.poll());
//        System.out.println(pq);
//        System.out.println(pq.poll());
//        System.out.println(pq);
//
//        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3));
//        System.out.println("list = " + list);
//        list.add(5);
//        list.add(5);
//        list.add(7);
//        System.out.println(list);


        PriorityQueue<Integer> pq = new PriorityQueue<>(Arrays.asList(1, 20, 4));
        System.out.println(pq);
        pq.offer(46);
        pq.offer(2);
        System.out.println(pq);
        System.out.println(pq.poll());
        System.out.println(pq.poll());
        System.out.println(pq.poll());
        System.out.println(pq.poll());
        System.out.println(pq.poll());
        System.out.println(pq.poll());
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    PriorityQueue<ListNode> pq = new PriorityQueue<>(((o1, o2) -> {
        return o1.val - o2.val;
    }));

    public ListNode mergeKLists(ListNode[] lists) {
        for (ListNode node : lists) {
            if (node != null) {
                pq.offer(node);
            }
        }
        ListNode dummy = new ListNode();
        ListNode cur = dummy;
        while (!pq.isEmpty()) {
            ListNode s = pq.poll();
            cur.next = s;
            cur = cur.next;
            s = s.next;
            if (s != null) {
                pq.offer(s);
            }
        }
        return dummy.next;
    }
}
